#define _CRT_SECURE_NO_WARNINGS 1
//#include <fstream>
//#include <iostream>
//#include <string>
//#include <sstream>
//#include<vector>
//#include<algorithm>
//using namespace std;
#include"Queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	//q->head->next = q->tail->next = NULL;
	q->head = q->tail = NULL;
	
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
	 }
	newnode->data = data;
	if (q->tail == NULL)
	{		
		q->head = q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
	}
	q->size++;
}

void QueuePop(Queue* q)
{
	assert(q);
	if (q->head->next == NULL)
	{
		q->head = q->tail = NULL;
	}
	else
	{
		QNode* tmp = q->head->next;
		free(q->head);
		q->head = tmp;
	}
	q->size--;
}

QDataType QueueFront(Queue* q)
{
	assert(q && q->head);
	return q->head->data;
}

QDataType QueueBack(Queue* q)
{
	assert(q && q->tail);
	return q->tail->data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->head == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (!QueueEmpty(q))
	{
		QueuePop(q);
	}
	q->head = q->tail = NULL;
}